package Action;

import java.util.ArrayList;
import java.util.List;

import Beans.Box;
import Beans.BoxUseDetail;
import Beans.Package;
import Beans.Terminal;
import DAO.BoxDao;
import DAO.PackageDao;
import DAO.TerminalDao;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class BoxAction extends ActionSupport {

	public int terminalID;
	public String terminalName;
	private int boxNum;
	private int boxID;
	private int fromNum;
	private int toNum;
	private int type;
	private List<Package> packages;
	private List<BoxUseDetail> boxesDetail = new ArrayList<BoxUseDetail>();
	private Terminal terminal;
	private Box box = new Box();

	ArrayList<Box> boxes;

	BoxDao boxDao = new BoxDao();
	PackageDao packageDao = new PackageDao();

	/**
	 * 新增柜子
	 * 
	 * @return
	 */
	public String registerBox() {
		boxDao = new BoxDao();
		for (int i = fromNum; i <= toNum; i++) {
			boxes = (ArrayList<Box>) boxDao.getBoxByBoxNum(terminalID, i);
			if (boxes.size() != 0) {
				boxDao.boxUpdate(boxes.get(0), terminalID, i, type);
			} else {
				boxDao.addBox(terminalID, i, type);
			}
		}
		return "success";
	}
	
	/**
	 * 修改柜子
	 * 
	 * @return
	 */
	public String updateBox() {
		box=boxDao.getBoxById(boxID);
		box.setBoxType(type);
		boxDao.updateBox(box);
		return "success";
	}
	/**
	 * 根据id获取柜子
	 * 
	 * @return
	 */
	public String getBoxById() {
		box=boxDao.getBoxById(boxID);
		return "success";
	}

	/**
	 * 规格使用比率详情
	 * 
	 * @return
	 */
	public String boxesUseDetails() {
		BoxUseDetail boxUseDetail = new BoxUseDetail();
		boxes = (ArrayList<Box>) boxDao.boxStatusInThisTerminal(terminalID);
		int a, b, c, d, w, x, y, z;
		a = b = c = d = w = x = y = z = 0;
		for (Box box : boxes) {
			if (box.getBoxType() == 1) {

				w = w
						+ packageDao.getPackagesInThisBox(terminalID,
								box.getBoxNum());
				++a;
			} else if (box.getBoxType() == 2) {
				x = x
						+ packageDao.getPackagesInThisBox(terminalID,
								box.getBoxNum());
				++b;
			} else if (box.getBoxType() == 3) {
				y = y
						+ packageDao.getPackagesInThisBox(terminalID,
								box.getBoxNum());
				++c;
			} else if (box.getBoxType() == 4) {
				z = z
						+ packageDao.getPackagesInThisBox(terminalID,
								box.getBoxNum());
				++d;
			} else {
				System.out.println("error");
			}
		}
		boxUseDetail.setTerminalName(terminalName);
		boxUseDetail.setSmallNum(d);
		boxUseDetail.setMediumNum(c);
		boxUseDetail.setBigNum(b);
		boxUseDetail.setBigestNum(a);
		boxUseDetail.setBigestTimes(w);
		boxUseDetail.setBigTimes(x);
		boxUseDetail.setMediumTimes(y);
		boxUseDetail.setSmallTimes(z);
		boxesDetail.add(boxUseDetail);
		return "success";
	}

	/**
	 * 根据中终端和箱号返回包裹的存取历史记录
	 * 
	 * @return
	 */
	public String packagesDetailByBoxNum() {
		packages = boxDao.getPacakgeDetailsByBoxNum(terminalID, boxNum);
		return "success";
	}

	/**
	 * 删除柜子
	 * 
	 * @return
	 */
	public String deleteBox() {
		box=boxDao.getBoxById(boxID);
		boxDao.delete(box);
		return "success";
	}

	/**
	 * 返回该终端的所有柜子
	 * 
	 * @return
	 */
	public String boxList() {
		TerminalDao terminalDao = new TerminalDao();
		boxes = (ArrayList<Box>) boxDao.boxStatusInThisTerminal(terminalID);
		terminal = terminalDao.getTerminalById(terminalID);
		return "boxList";
	}

	/**
	 * 设置故障柜子
	 * 
	 * @return
	 */
	public String setBoxFault() {
		boxDao.setBoxFault(terminalID, boxNum);
		return "success";
	}

	/**
	 * 设置柜子可用
	 * 
	 * @return
	 */
	public String setBoxAvailable() {
		boxDao.setBoxAvailable(terminalID, boxNum, -1);
		return "success";
	}

	// Boring Setter and Getter
	public ArrayList<Box> getBoxes() {
		return boxes;
	}

	public void setBoxes(ArrayList<Box> boxes) {
		this.boxes = boxes;
	}

	public int getTerminalID() {
		return terminalID;
	}

	public void setTerminalID(int terminalID) {
		this.terminalID = terminalID;
	}

	public int getBoxNum() {
		return boxNum;
	}

	public void setBoxNum(int boxNum) {
		this.boxNum = boxNum;
	}

	public List<Package> getPackages() {
		return packages;
	}

	public void setPackages(List<Package> packages) {
		this.packages = packages;
	}

	public Terminal getTerminal() {
		return terminal;
	}

	public void setTerminal(Terminal terminal) {
		this.terminal = terminal;
	}

	public String getTerminalName() {
		return terminalName;
	}

	public void setTerminalName(String terminalName) {
		this.terminalName = terminalName;
	}

	public List<BoxUseDetail> getBoxesDetail() {
		return boxesDetail;
	}

	public void setBoxesDetail(List<BoxUseDetail> boxesDetail) {
		this.boxesDetail = boxesDetail;
	}

	public int getBoxID() {
		return boxID;
	}

	public void setBoxID(int boxID) {
		this.boxID = boxID;
	}

	public Box getBox() {
		return box;
	}

	public void setBox(Box box) {
		this.box = box;
	}

	public int getFromNum() {
		return fromNum;
	}

	public void setFromNum(int fromNum) {
		this.fromNum = fromNum;
	}

	public int getToNum() {
		return toNum;
	}

	public void setToNum(int toNum) {
		this.toNum = toNum;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}
}
